package com.sihan.swvp.action.video;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.Page;
import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.common.CommonUtil;
import com.sihan.swvp.entity.video.VideoManage;
import com.sihan.swvp.entity.video.VideoSyn;
import com.sihan.swvp.service.sys.LogService;
import com.sihan.swvp.service.video.VideoManagerService;
import com.sihan.swvp.service.video.VideoMatchService;

/**
 * 视频管Action
 * @author jch
 *
 */
@Controller
@RequestMapping("/videomanager")
public class VideoManagerAction extends BaseAction{
	
	@Autowired
	VideoManagerService service;
	
	@Autowired
	private VideoMatchService matchService;
	
	@Autowired
	public LogService logService;
	
	/**
	 * 获取视频
	 * @param limit 每页数量
	 * @param page 页数
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getVideo")
	public Object getVideo(@RequestParam int limit,@RequestParam int page,
			String taskName,String bz,String js,String video){
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("taskc", taskName); //任务名称
		params.put("bzname", bz); //班组民称
		params.put("jsname", js); //角色名称
		params.put("vname", video); //视频名称
		params.put("starttime", getParameter("startTime"));//开始时间
		params.put("endtime", getParameter("endTime"));//结束时间
		Page<Map<String,Object>> list = service.getVideo(page,limit,params);
		
		return pageData(list);
	}
	
	
	@ResponseBody
	@RequestMapping(value="/getVideoPic")
	public void getVideoPic(HttpServletResponse response,@RequestParam(required = true) int videoid,Double time){
		time = time == null ? 0 : time;
		ServletOutputStream output = null;
		try {
			response.setHeader("Content-Type","image/jped");
			output = response.getOutputStream();
			byte[] bytes= service.getVideoPic(videoid, time);
			output.write(bytes);
		    output.flush();
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
		finally{
			CommonUtil.close(output);
		}
	}
	
	
	@ResponseBody
	@RequestMapping(value="/addTaskMatch")
	public int addTaskMatch(Integer video_id,String task_id,Integer role_id,
			Integer stage_id,Integer bz_id,String mark,Integer delId){
		try {
			VideoManage vm = new VideoManage();
			vm.setRole_id(role_id);
			vm.setTask_id(task_id);
			vm.setStage_id(stage_id);
			vm.setVideo_id(video_id);
			vm.setBz_id(bz_id);
			vm.setUpuser(getLoginUser().getId());
			matchService.addVideoManage(vm,mark,delId,null);
			
			return 1;
		} catch (Exception e) {
			return -1;
		}
	}
	
	@ResponseBody
	@RequestMapping(value="/getVideoByTaskid")
	public List<Map<String, Object>> getVideoByTaskid(String taskId,
			Integer roleId,Integer stageId){
		return matchService.getVideoByTaskid(taskId,roleId,stageId);
	}
	
	
	@ResponseBody
	@RequestMapping(value="/deleteVideo")
	public Object deleteVideo(Integer videoid){
		List<Map<String,Object>> wgbjs = service.getWgbj(videoid);
		try {
			if(wgbjs.isEmpty()){
				VideoSyn v = service.getVideoById(videoid);
				service.deleteVideo(v);
				logService.addUserLog("删除视频", "视频", getLoginUser().getId(), "删除视频："+v.getName(),getRemoteAddr(),1,0);
			}
			else{
				
				return failMsg("视频为“违规标记”视频，不能删除。");
			}
		} catch (Exception e) {
			// TODO: handle exception
			if(wgbjs.isEmpty()){
				VideoSyn v = service.getVideoById(videoid);
				logService.addUserLog("删除视频", "视频", getLoginUser().getId(), "删除视频："+v.getName(),getRemoteAddr(),0,0);
			}
			return failMsg("服务器异常");
		}
		
		
		return successMsg();
	}
}
